<?php

namespace app\model\order;

use app\model\user\User;
use think\Model;

/**
 * t_recharge_order 用户余额充值订单
 * @property integer $id id(主键)
 * @property string $recharge_no 充值订单号
 * @property string $pay_no 支付订单号
 * @property integer $user_id 充值用户
 * @property string $username 充值用户名
 * @property string $amount 充值金额
 * @property integer $pay_way 支付方式 1:微信 2:支付宝
 * @property integer $status 状态 1:待支付 2:支付成功 3:支付失败 4:超时关闭
 * @property string $third_no 三方订单号
 * @property string $return_msg 三方返回信息
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class RechargeOrder extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';
    
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_recharge_order';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    //支付方式 1:微信 2:支付宝 3:余额
    const PAY_WAY_WECHAT_PAY = 1; //微信
    const PAY_WAY_ALI_PAY = 2; //支付宝

    const PAY_WAY_TEXT_MAP = [
        self::PAY_WAY_WECHAT_PAY => "微信",
        self::PAY_WAY_ALI_PAY => "支付宝",
    ];
    //状态 1:待支付 2:支付成功 3:支付失败 4:超时关闭
    const STATUS_WAIT_PAID = 1;     //待支付
    const STATUS_PAID = 2;     //支付成功
    const STATUS_PAID_ERROR = 3;     //支付失败
    const STATUS_CLOSE = 4; //超时关闭

    const STATUS_TEXT_MAP = [
        self::STATUS_WAIT_PAID => "待支付",
        self::STATUS_PAID => "支付成功",
        self::STATUS_PAID_ERROR => "支付失败",
        self::STATUS_CLOSE => "超时关闭",
    ];
    protected $append = [
        'status_text',
        'pay_way_text',
    ];


    public function getPayWayTextAttr($value, $data)
    {
        return self::PAY_WAY_TEXT_MAP[$data['pay_way']] ?? "";
    }
    public function getStatusTextAttr($value, $data)
    {
        return self::STATUS_TEXT_MAP[$data['status']] ?? "";
    }

    public function user()
    {
        return $this->belongsTo(User::class, "user_id", "id");
    }
}
